import React, { Component } from "react";
import myPubSub from "@/utils/myPubSub";
import { getSecondCategoryListApi } from "@/api/goods";
import style from "./index.module.scss";

export default class index extends Component {
  state = {
    secondList: [],
  };

  // 获取第二级的分类
  async getSecondCategoryList(id) {
    let res = await getSecondCategoryListApi({ id });
    if (res.data.code === 200) {
      console.log("二级分类的数据：", res.data.subcate);
      this.setState({
        secondList: res.data.subcate.subcategory,
      });
    }
  }

  componentDidMount() {
    console.log("myPubSub", myPubSub);
    if (!myPubSub.hasSubscribers("chuanFirstId")) {
      myPubSub.subscribe("chuanFirstId", (id) => {
        this.getSecondCategoryList(id);
      });
    } 
  }

  render() {
    return (
      <div id="sss" className={this.props.widthcls}>
        <ul>
          {this.state.secondList.map((item) => (
            <li key={item.id}>
              <h3>{item.name}</h3>
              <ul className={style.secondUl}>
                {item.subNavs.map((subItem) => (
                  <li key={subItem.id}>
                    <img className={style.img} src={subItem.pic} />
                    <p>{subItem.name}</p>
                  </li>
                ))}
              </ul>
            </li>
          ))}
        </ul>
      </div>
    );
  }
}
